
package com.xifan.myaccount.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hb.views.PinnedSectionListView.PinnedSectionListAdapter;
import com.xifan.myaccount.R;
import com.xifan.myaccount.entity.RecordDetail;
import com.xifan.myaccount.entity.RecordType;
import com.xifan.myaccount.util.SmartType;
import com.xifan.myaccount.util.Util;

import java.text.SimpleDateFormat;
import java.util.List;

public class DetailListAdapter extends BaseAdapter implements PinnedSectionListAdapter {

    private List<RecordDetail> list;
    private List<RecordType> typeList;
    private Context mContext;
    private boolean isMultiMode;
    private int colorBlue;
    private int colorWhite;
    private int colorBlack;
    private int cardPadding;
    private Drawable cardBackground;

    private SimpleDateFormat dateFomatter = Util.getDateFormatter("MM-dd ccc");

    public DetailListAdapter(Context context) {
        mContext = context;
        typeList = SmartType.getTypeList(0);
        Resources res = mContext.getResources();
        colorBlue = res.getColor(R.color.holo_blue);
        colorWhite = res.getColor(R.color.white);
        colorBlack = res.getColor(R.color.black);
        cardBackground = res.getDrawable(R.drawable.card_background);
        cardPadding = res.getDimensionPixelOffset(R.dimen.card_padding);
    }

    public DetailListAdapter(Context context, List<RecordDetail> list) {
        this(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        return position < list.size() ? list.get(position).getPinnedTag() : 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setCheckItem(int position, boolean isChecked) {
        list.get(position).setState(isChecked ? 1 : 0);
    }

    public void setCheckMode(boolean value) {
        isMultiMode = value;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) { // common item
                view = View.inflate(mContext, R.layout.view_detail_list_item, null);
                holder.typeText = (TextView) view.findViewById(R.id.account_type);
                holder.dateText = (TextView) view.findViewById(R.id.account_date);
                holder.moneyText = (TextView) view.findViewById(R.id.account_money);
                holder.imgIcon = (ImageView) view.findViewById(R.id.account_extra_image);
                view.setTag(holder);
            } else {
                view = View.inflate(mContext, R.layout.view_detail_list_header, null);
                holder.dateText = (TextView) view.findViewById(R.id.header_date);
                view.setTag(holder);
            }
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (position >= list.size())
            return view;
        RecordDetail detail = list.get(position);
        if (getItemViewType(position) == 0) { // common item
            int typeId = detail.getRecordType();

            RecordType currentType = SmartType.getTypeInfoById(typeList, typeId);

            holder.typeText.setText(currentType.typeName);
            holder.dateText.setText(detail.getDate());
            holder.moneyText.setText(detail.getMoney());

            if (!TextUtils.isEmpty(detail.getPicUri())) {
                holder.imgIcon.setVisibility(View.VISIBLE);
            } else {
                holder.imgIcon.setVisibility(View.GONE);
            }
            // update checked item view
            if (isMultiMode) {
                if (detail.getState() == 1) {
                    view.setBackgroundColor(colorBlue);
                    holder.typeText.setTextColor(colorWhite);
                    holder.dateText.setTextColor(colorWhite);
                    holder.moneyText.setTextColor(colorWhite);
                } else {
                    view.setBackground(cardBackground);
                    int padding = cardPadding;
                    view.setPadding(padding, 0, padding, 0);
                    holder.typeText.setTextColor(colorBlack);
                    holder.dateText.setTextColor(colorBlack);
                    holder.moneyText.setTextColor(colorBlack);
                }
            } else if (detail.getState() == 1) {
                view.setBackground(cardBackground);
                int padding = cardPadding;
                view.setPadding(padding, 0, padding, 0);
                holder.typeText.setTextColor(colorBlack);
                holder.dateText.setTextColor(colorBlack);
                holder.moneyText.setTextColor(colorBlack);
            }
        } else {
            holder.dateText.setText(dateFomatter.format(Util.parseToDate(detail.getDate())
                    .getTime()));
        }

        return view;
    }

    public final class ViewHolder {
        TextView typeText;
        TextView dateText;
        TextView moneyText;
        ImageView imgIcon;
    }

}
